package cn.itmantzy.system.controller;

import cn.itmantzy.basic.util.AjaxResult;
import cn.itmantzy.basic.util.PageList;
import cn.itmantzy.system.domain.DicType;
import cn.itmantzy.system.query.DicTypeQuery;
import cn.itmantzy.system.service.IDicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dic")
public class DicTypeController {

    @Autowired
    private IDicTypeService dicTypeService;

    @GetMapping("/list")
    public List<DicType> findAll(){
        return dicTypeService.queryAll();
    }

    @PostMapping("/page")
    public PageList<DicType> findByQuery(@RequestBody DicTypeQuery query){
        return dicTypeService.queryPage(query);
    }

    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody DicType dicType){
        try {
            if(dicType.getId() == null){
                dicTypeService.add(dicType);
            }else{
                dicTypeService.update(dicType);
            }
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dicTypeService.remove(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult()
                    .setSuccess(false)
                    .setMessage("操作失败，请稍后重试！！！");
        }
    }

}
